package cn.ococci.dm.Main.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.ococci.dm.Main.Model.UnitModel;
import cn.ococci_dm.R;

/**
 * Created by cyan on 15/12/30.
 */
public class SelectedFloorAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private Context context;
    private List<UnitModel> selectedFloorList;

    private OperatorFloor operatorFloor;

    public SelectedFloorAdapter(Context context, List<UnitModel> data, OperatorFloor operatorFloor, ListView listView){

        this.context = context;
        this.selectedFloorList = data;
        this.operatorFloor = operatorFloor;

        listView.setOnItemClickListener(this);
        listView.setAdapter(this);
    }


    @Override
    public int getCount() {
        return selectedFloorList.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if( convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_modify_unit_show_item,parent,false);
            holder = new ViewHolder();

            holder.textView = (TextView) convertView.findViewById(R.id.unitNameTv);
            holder.addUnitImage = (ImageView) convertView.findViewById(R.id.addUnitImage);
            holder.deleteImage = (ImageView) convertView.findViewById(R.id.deleteImage);

            convertView.setTag(holder);
        }else {

            holder = (ViewHolder) convertView.getTag();
        }


        if (position == selectedFloorList.size()){
            holder.textView.setVisibility(View.GONE);
            holder.deleteImage.setVisibility(View.GONE);
            holder.addUnitImage.setVisibility(View.VISIBLE);

        }else {

            holder.addUnitImage.setVisibility(View.GONE);

            holder.textView.setVisibility(View.VISIBLE);
            holder.deleteImage.setVisibility(View.VISIBLE);
            holder.textView.setText(selectedFloorList.get(position).getUnitName());

        }


        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (position == selectedFloorList.size()){

            operatorFloor.addFloor();
        }else {

            operatorFloor.deleteFloor(position);
            SelectedFloorAdapter.this.notifyDataSetChanged();
        }

    }


    public interface OperatorFloor{

         void addFloor();

         void deleteFloor(int pos);

    }



    public static class ViewHolder{

        TextView textView;
        ImageView addUnitImage;
        ImageView deleteImage;

    }
}
